#!/usr/bin/env python
# coding=utf-8
'''
Created on 2010-7-27

@author: Jason.q.yan
'''
import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import math
import urllib2
from Route import *

pageNo_re = re.compile(r'''
        <li\sclass="index_all">1/([\s\S]+?)</li>
        ''', re.X|re.S)
routeDiv_re = re.compile(r'''
       <td\s+?class="model_house\s+?model_normal">([\s\S]+?)预订</button></td>
               ''', re.X|re.S)
 
routeUrl_re = re.compile(r'''
        <a\s*href="([^"]+)
        ''', re.X|re.S)
schedule_re =  re.compile(r'''
        <td\scolspan="\d"\sclass="model_blue">行程安排：([\s\S]+?)</table>
        ''', re.X|re.S)
                        
server_urls = [('自由行','http://www.0731uu.com/lineListAction.shtml?curPage=%d',)
            ]
def do_crawl():
    try:
        for vacation_type, server_url in server_urls:
            html = urllib2.urlopen(server_url % 1).read().decode('gbk','ignore').encode('utf-8')
            
            #获取总线路数算出总页数
            pageTotal = int(pageNo_re.search(html).group(1))
            #根据总页数，遍历所有线路列表页
           
            for pageNo in range(1,pageTotal+1):
                #获取所有描述线路的HTML片段 
                html = urllib2.urlopen(server_url % pageNo).read().decode('gbk','ignore').encode('utf-8')
                route_list = routeDiv_re.findall(html)
                #遍历，解析
                for div in route_list:
                    route = Route()
                    route.site = 29
                    route.supplier_url = 'http://www.0731uu.com/'
                    route.supplier = "湖南中青旅"
                    route.go_t = route.back_t = "-"
                    all=re.compile(r'''
                    <a\s+?href="([\s\S]+?)">([\s\S]+?)</a></td>\s+?<td>([\s\S]+?)</td>\s+?<td>([\s\S]+?)</td>([\s\S]+?)￥([\s\d]+?)</span>
                                ''', re.X|re.S).search(div)
                    route.title = all.group(2)
                    route.price = all.group(6)
                    route.outcity = all.group(4)
                    #detail = re.compile(r'''线路简介：</b>([\s\S]+)*</dd>''', re.X|re.S).search(div).group(1)
                    #if detail:
                      #route.detail =detail
                    #route.img_url= 
                    route.telphone="0731-82222966"
                    #route.days = re.compile(r'''行程天数：<span>([^</span>.*]+?)</span>''', re.X|re.S).search(html).group(1)
                    #route.dest = re.compile(r'''途径城市：<span>([^</span>.*]+?)</span>''', re.X|re.S).search(html).group(1)
                    route.rout_url = route.supplier_url+all.group(1)
                    route.dates = all.group(3)
                    #根据URL获取线路详细页面的HTML
                    temp = urllib2.urlopen(route.rout_url).read().decode('gbk','ignore').encode('utf-8')
                    #进一步解析 
                    do_parse(temp,route)
                    print >>sys.stderr, 'updating', route.supplier, route.title
                    if route.schedules and len(route.schedules)>0:
                        route.days=len(route.schedules)
                    route.updRoute()
                    route.updRoute_Schedules()
                
    except:
        print traceback.format_exc(sys.exc_info())

#解析每一天的行程安排
def do_parse(html,route):
    try:
        detail = re.compile(r'''线路特色</h6>([\s\S]+)</div>\s+?<div\sclass="model_product_table\smodel_noneborer"><h6>服务标准</h6>''', re.X|re.S).search(html).group(1)
        if detail:
            route.detail =detail
        img_url = re.compile(r'''
<td\swidth="24%"\srowspan="4"\sclass="model_thimg2"><img\ssrc="/([\s\S]+)"\sonerror="this.style.display='none'"\s/></td>\s+<th\swidth="\d+?%">线路名称：</th>                        ''', re.X|re.S).search(html).group(1)
        
        route.img_url=route.supplier_url+img_url
    except: AttributeError
    schedule_list = schedule_re.findall(html)
    for parthtml in schedule_list:
        schedule = Schedule()
        schedule.s_num = re.compile(r'''第(\d+)天''', re.X|re.S).search(parthtml).group(1)
        dest_temp = re.compile(r'''第(\d+)天\s([\s\S]+?)\s?-\s?([\s\S]+?)</td>''', re.X|re.S).search(parthtml)
        if dest_temp:
            schedule.s_place = dest_temp.group(2)
        brief_temp = re.compile(r'''行程内容：</th>\s+?<td\scolspan="3">([\s\S]+?)</td>''', re.X|re.S).search(parthtml)
        if brief_temp:
            schedule.s_brief = brief_temp.group(1)
        try:
            eat = re.compile(r'''<th\swidth="11%">用餐：</th>\s+?<td\swidth="24%">\s+([\s\S]+?)\s+</td>\s+?</tr>''', re.X|re.S).search(parthtml)
            if str(eat.group(1)).count('早')>0:
                schedule.s_brf = '1'
            if ( str(eat.group(1)).count('午')>0|str(eat.group(1)).count('中')>0):
                schedule.s_lunch = '1'
            if str(eat.group(1)).count('晚')>0:
                schedule.s_dinner = '1'
        except: AttributeError
        route.schedules.append(schedule)
    return 

if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()
